module mod_workspace
  ! allocate workspace might be used by other subroutine

  !donot vary as time goes

  real(8),allocatable,dimension(:)       ::kx,ky,xi,yi 
  real(8),allocatable,dimension(:,:)     ::kk,zi    
  real(8),allocatable,dimension(:,:,:)   ::expkz0kn,hnr_nfac,hnr_nfacdh

  !vary as time goes
  real(8),allocatable,dimension(:,:)     ::bzi,bzis,ts
  complex(8),allocatable,dimension(:,:)  ::fftcomplex2

  ! field
  real(8),allocatable,dimension(:,:,:)::ex,ey,ez,hx,hy,hz,cex,cey,cez,chx,chy,chz
contains

  subroutine init_field()
    use mod_para
    implicit none
    integer::ierr
    allocate(ex(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);ex=0.0;
    allocate(ey(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);ey=0.0;
    allocate(ez(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);ez=0.0;
    allocate(hx(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);hx=0.0;
    allocate(hy(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);hy=0.0;
    allocate(hz(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);hz=0.0;

    allocate(cex(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);cex=0.0;
    allocate(cey(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);cey=0.0;
    allocate(cez(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);cez=0.0;
    allocate(chx(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);chx=0.0;
    allocate(chy(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);chy=0.0;
    allocate(chz(nx1-1:nx2+1,ny1-1:ny2+1,nz1-1:nz2+1),stat=ierr);chz=0.0;

  end subroutine init_field


  subroutine init_upward_workspace(nexpand)
    use mod_para
    implicit none
    integer::nexpand,ierr
    allocate(expkz0kn(nxi/2+1,nyi,0:nexpand),stat=ierr)
    allocate(hnr_nfac(nxi,nyi,1:nexpand),stat=ierr)
    allocate(hnr_nfacdh(nxi,nyi,1:nexpand),stat=ierr)
    allocate(kk(nxi,nyi),stat=ierr)
    allocate(kx(nxi),stat=ierr)
    allocate(ky(nyi),stat=ierr)
    allocate(zi(nxi,nyi))
    allocate(xi(nxi))
    allocate(yi(nyi))
    allocate(bzi(nxi,nyi))
	allocate(ts(nxi,nyi))
    allocate(bzis(nxi,nyi))
    allocate(fftcomplex2(nxi/2+1,nyi),stat=ierr)
  endsubroutine init_upward_workspace

  subroutine delete_upward_workspace()
    deallocate(expkz0kn)
    deallocate(hnr_nfac)
    deallocate(hnr_nfacdh)
    deallocate(kk)
    deallocate(kx)
    deallocate(ky)
    deallocate(xi)
    deallocate(yi)
    deallocate(zi)
    deallocate(bzi)
    deallocate(bzis)
    deallocate(fftcomplex2)
  endsubroutine delete_upward_workspace



end module mod_workspace


